package exercise;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.stage.Stage;

public class DemoChoiceBox extends Application{

	@Override
	public void start(Stage primaryStage) throws Exception {
		
		Scene scene = new Scene(cteateUi());
		primaryStage.setTitle("JavaFx");
		primaryStage.setWidth(500);
		primaryStage.setHeight(400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	private Parent cteateUi() {
		ChoiceBox<String> cb = new ChoiceBox<>();
		
		ObservableList<String> list = FXCollections.observableArrayList();
		list.add(".pdf");
		list.add(".doc");
		list.add(".docx");
		
		cb.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				// TODO Auto-generated method stub
				System.out.println(oldValue+"/"+newValue);				
	
			}
		});
		cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				System.out.println(oldValue+' '+newValue);				
			}
		});
		
		cb.setItems(list);
		
		return cb;
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
}
